<template>
  <a-spin :spinning="confirmLoading">
    <a-form-model ref="form" layout="inline" :model="model" :rules="validatorRules">
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item prop="lotteryState">
            <a-checkbox v-model="model.lotteryState">{{ $t('彩票冻结激活') }}</a-checkbox>
          </a-form-model-item>
        </a-col>
        <a-col :span="24" style="padding-left: 40px">
          <a-form-model-item :label="$t('彩票冻结')" prop="freezeDay">
            <i18n path="lottery-freeze" tag="span">
              <a-input-number
                ref="freezeDay"
                place="Days"
                :disabled="!model.lotteryState"
                v-model="model.freezeDay"
                style="margin: 0 2px"
                :min="1"
              />
            </i18n>
          </a-form-model-item>
        </a-col>
        <a-col :span="24" style="padding-left: 40px">
          <a-form-model-item :label="$t('彩票激活')" prop="activateValue">
            <i18n path="lottery-activation" tag="span">
              <a-input-number
                ref="activateValue"
                place="value"
                :disabled="!model.lotteryState"
                v-model="model.activateValue"
                style="margin: 0 2px"
                :min="0"
              />
            </i18n>
          </a-form-model-item>
        </a-col>
        <a-col :span="24" style="padding-left: 120px; margin-bottom: 30px" v-if="!disableSubmit">
          <a-button type="primary" @click="saveFreeze">{{ $t('保存') }}</a-button>
        </a-col>
      </a-row>
    </a-form-model>
    <a-form-model ref="iform" layout="inline" :model="model" :rules="validatorRules">
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item prop="growthState">
            <a-checkbox v-model="model.growthState">{{ $t('彩票增长') }}</a-checkbox>
            <a-tooltip>
              <template slot="title">{{ $t('场地可根据需要设置彩票增长活动，促进客户消费，增强客户与场地的黏着度。') }}</template>
              <a-icon type="question-circle"/>
            </a-tooltip>
          </a-form-model-item>
        </a-col>
        <a-col :span="8" style="padding-left: 40px">
          <a-form-model-item :label="$t('月增长比例')" prop="rate">
            <a-input-number
              ref="rate"
              place="Days"
              :disabled="!model.growthState"
              v-model="model.rate"
              style="margin-right: 2px; width: 80px"
              :min="0"
            />%
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item prop="type" style="margin-bottom: 5px" :label="$t('发放方式')">
            <a-radio-group v-model="model.type" ref="type" :disabled="!model.growthState">
              <a-radio :value="0">{{ $t('人工领取') }}
                <a-tooltip>
                  <template slot="title">{{ $t('满足条件的用户需要到吧台的“彩票增长”功能中刷卡领取。') }}</template>
                  <a-icon type="question-circle" style="margin-left: 3px" />
                </a-tooltip>
              </a-radio>
              <a-radio :value="1">{{ $t('自动发放') }}
                <a-tooltip>
                  <template slot="title">{{ $t('系统每个月在设定日期的00:00:00开始依次为满足条件的客户自动发放赠送的彩票值。') }}</template>
                  <a-icon type="question-circle" style="margin-left: 3px" />
                </a-tooltip>
              </a-radio>
            </a-radio-group>
          </a-form-model-item>
        </a-col>
        <a-col :span="9" style="padding-left: 40px">
          <a-form-model-item v-if="model.type === 0" :label="$t('领取时间')" prop="time">
            <i18n path="growth-time" tag="span">
              <a-input-number
                ref="claimStartTime"
                place="start"
                :disabled="!model.growthState"
                v-model="model.claimStartTime"
                style="margin: 0 2px"
                :max="31"
                :min="1"
              />
              <a-input-number
                ref="claimEndTime"
                place="end"
                :disabled="!model.growthState"
                v-model="model.claimEndTime"
                style="margin: 0 2px; width: 80px"
                :max="31"
                :min="1"
              />
            </i18n>
          </a-form-model-item>
          <a-form-model-item v-if="model.type === 1" :label="$t('发放日期')" prop="time">
            <i18n path="release-time" tag="span">
              <a-input-number
                ref="releaseTime"
                place="start"
                :disabled="!model.growthState"
                v-model="model.releaseTime"
                style="margin: 0 2px"
                :max="28"
                :min="1"
              />
            </i18n>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item v-if="model.type === 0" :label="$t('')" prop="stopTime">
            <i18n path="growth-duration" tag="span">
              <a-input-number
                ref="stopTime"
                place="duration"
                :disabled="!model.growthState"
                v-model="model.stopTime"
                style="margin: 0 2px; width: 80px"
                :min="1"
              />
            </i18n>
          </a-form-model-item>
        </a-col>
        <a-col :span="24" style="padding-left: 120px" v-if="!disableSubmit">
          <a-button type="primary" @click="saveGrowth">{{ $t('保存') }}</a-button>
        </a-col>
      </a-row>
    </a-form-model>
  </a-spin>
</template>

<script>
import { isEmpty, httpAction, getAction } from '@/api/manage'

export default {
  name: 'RuleLotteryForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      disableSubmit: false,
      model: {
        freezeId: '',
        freezeType: 10,
        lotteryState: false,
        freezeDay: 30,
        activateValue: 1,
        growthId: '',
        growthState: false,
        rate: 1,
        claimStartTime: 1,
        claimEndTime: 31,
        stopTime: 30,
        type: 0,
        releaseTime: 1,
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
      },
    }
  },
  methods: {
    edit(record) {
      let that = this
      that.visible = true
      that.confirmLoading = false
      that.model = Object.assign({}, that.model, record)
      that.getRuleFreeze()
      that.getGrowth()
    },
    getRuleFreeze() {
      let that = this
      getAction('/member/bisMemberRuleFreeze/list', { memberTypeId: that.model.id }).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            let dtl = res.result.records[0]
            that.$set(that.model, 'freezeId', dtl.id)
            that.$set(that.model, 'freezeType', dtl.type)
            that.$set(that.model, 'lotteryState', dtl.state ? true : false)
            that.$set(that.model, 'freezeDay', dtl.freezeDay)
            that.$set(that.model, 'activateValue', dtl.activateValue)
          } else {
            that.$set(that.model, 'freezeId', '')
            that.$set(that.model, 'freezeType', 10)
            that.$set(that.model, 'lotteryState', false)
            that.$set(that.model, 'freezeDay', 30)
            that.$set(that.model, 'activateValue', 1)
          }
        } else {
          that.$set(that.model, 'freezeId', '')
          that.$set(that.model, 'freezeType', 10)
          that.$set(that.model, 'lotteryState', false)
          that.$set(that.model, 'freezeDay', 30)
          that.$set(that.model, 'activateValue', 1)
          that.$message.error(res.message)
        }
      })
    },
    getGrowth() {
      let that = this
      getAction('/member/bisMemberRuleLottery/list', { memberRuleId: that.model.id }).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            let dtl = res.result.records[0]
            that.$set(that.model, 'growthId', dtl.id)
            that.$set(that.model, 'growthState', dtl.state=='Y' ? true : false)
            that.$set(that.model, 'rate', dtl.rate)
            that.$set(that.model, 'claimStartTime', dtl.claimStartTime)
            that.$set(that.model, 'claimEndTime', dtl.claimEndTime)
            that.$set(that.model, 'stopTime', dtl.stopTime)
            that.$set(that.model, 'type', dtl.type)
            that.$set(that.model, 'releaseTime', dtl.releaseTime || 1)
          } else {
            that.$set(that.model, 'growthId', '')
            that.$set(that.model, 'growthState', false)
            that.$set(that.model, 'rate', 1)
            that.$set(that.model, 'claimStartTime', 1)
            that.$set(that.model, 'claimEndTime', 31)
            that.$set(that.model, 'stopTime', 30)
            that.$set(that.model, 'type', 0)
            that.$set(that.model, 'releaseTime', 1)
          }
        } else {
          that.$set(that.model, 'growthId', '')
          that.$set(that.model, 'growthState', false)
          that.$set(that.model, 'rate', 1)
          that.$set(that.model, 'claimStartTime', 1)
          that.$set(that.model, 'claimEndTime', 31)
          that.$set(that.model, 'stopTime', 30)
          that.$set(that.model, 'type', 0)
          that.$set(that.model, 'releaseTime', 1)
          that.$message.error(res.message)
        }
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    saveFreeze() {
      let that = this
      that.$refs.form.validate((valid) => {
        if (valid) {
          if (that.model.lotteryState && isEmpty(that.model.freezeDay)) {
            that.$message.warn(that.$t('请设置冻结规则'))
            that.$refs.freezeDay.focus()
            return
          }
          if (that.model.lotteryState && isEmpty(that.model.activateValue)) {
            that.$message.warn(that.$t('请设置激活规则'))
            that.$refs.activateValue.focus()
            return
          }
          let httpurl = ''
          let method = ''
          if (that.model.freezeId) {
            httpurl = '/member/bisMemberRuleFreeze/edit'
            method = 'put'
          } else {
            httpurl = '/member/bisMemberRuleFreeze/add'
            method = 'post'
          }
          let query = {
            memberTypeId: that.model.id,
            type: that.model.freezeType,
            state: that.model.lotteryState ? 1 : 0,
            freezeDay: that.model.freezeDay,
            activateValue: that.model.activateValue,
          }
          if (that.model.freezeId) {
            query.id = that.model.freezeId
          }
          httpAction(httpurl, query, method).then((res) => {
            if (res.success) {
              that.getRuleFreeze()
              that.$message.success(res.message)
            } else {
              that.$message.warning(res.message)
            }
          })
        }
      })
    },
    saveGrowth() {
      let that = this
      that.$refs.iform.validate((valid) => {
        if (valid) {
          if (that.model.growthState && (that.model.rate == undefined || that.model.rate == null || that.model.rate == '')) {
            that.$message.warn(that.$t('请设置'))
            that.$refs.rate.focus()
            return
          }
          if (that.model.growthState && that.model.type === 0 && isEmpty(that.model.claimStartTime)) {
            that.$message.warn(that.$t('请设置'))
            that.$refs.claimStartTime.focus()
            return
          }
          if (that.model.growthState && that.model.type === 0 && isEmpty(that.model.claimEndTime)) {
            that.$message.warn(that.$t('请设置'))
            that.$refs.claimEndTime.focus()
            return
          }
          if (that.model.growthState && that.model.type === 0 && isEmpty(that.model.stopTime)) {
            that.$message.warn(that.$t('请设置'))
            that.$refs.stopTime.focus()
            return
          }
          if (that.model.growthState && that.model.type === 1 && isEmpty(that.model.releaseTime)) {
            that.$message.warn(that.$t('请设置'))
            that.$refs.releaseTime.focus()
            return
          }
          let httpurl = ''
          let method = ''
          if (that.model.growthId) {
            httpurl = '/member/bisMemberRuleLottery/edit'
            method = 'put'
          } else {
            httpurl = '/member/bisMemberRuleLottery/add'
            method = 'post'
          }
          let query = {
            memberRuleId: that.model.id,
            rate: that.model.rate,
            state: that.model.growthState ? 'Y' : 'N',
            claimStartTime: that.model.claimStartTime,
            claimEndTime: that.model.claimEndTime,
            stopTime: that.model.stopTime,
            type: that.model.type,
            releaseTime: that.model.releaseTime
          }
          if (that.model.growthId) {
            query.id = that.model.growthId
          }
          httpAction(httpurl, query, method).then((res) => {
            if (res.success) {
              that.getGrowth()
              that.$message.success(res.message)
            } else {
              that.$message.warning(res.message)
            }
          })
        }
      })
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
.cus-form {
  margin-top: 10px;
  margin-left: 40px;
}
</style>